package com.rbac.api.service.common;


import com.rbac.api.controller.enums.HintMessageEunm;
import com.rbac.api.controller.response.Response;
import com.rbac.api.mybatis.mapper.AdminMenusMapper;
import com.rbac.api.mybatis.mapper.AdminRolesMapper;
import com.rbac.api.mybatis.mapper.AdminUsersMapper;
import com.rbac.utils.commUtils.LogUtil;
import com.rbac.utils.commUtils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


///修改状态公共service
@Service
public class SaveStatusCommService {
    ///操作日志
    @Autowired
    private InsActionLogService insActionLogService;

    //日志操作
    private static final LogUtil logger = LogUtil.getLogger(SaveStatusCommService.class);

    //管理员表
    @Autowired
    private AdminUsersMapper adminUsersMapper;

    ///菜单表
    @Autowired
    private AdminMenusMapper adminMenusMapper;

    ///角色表mapper
    @Autowired
    private AdminRolesMapper adminRolesMapper;


    ///更新状态操作
    ///操作的数据表、请求的接口用途、操作人账号、修改后的状态、操作人编号、操作时间、数据id、当前数据状态、request
    public final Object common(String actionType,String actionNameStr,String actionAccount, Integer upStatus, String lastUpdateUid, Date lastUpdateTime, Integer id, Integer nowStatus, HttpServletRequest request) throws Exception{
        try{
            ///默认修改状态
            Integer saveRes = 0;
            ///判断操作那张表
            switch(actionType){
                case "user":
                    saveRes = this.adminUsersMapper.saveStatusByIdAndAccountMapper(upStatus,lastUpdateUid,lastUpdateTime,id,nowStatus);
                    break;
                case "menu":
                    saveRes = this.adminMenusMapper.saveStatusByIdAndStatusMapper(upStatus,lastUpdateUid,lastUpdateTime,id,nowStatus);
                    break;
                case "role":
                    saveRes = this.adminRolesMapper.saveStatusByIdAndStatusMapper(upStatus,lastUpdateUid,lastUpdateTime,id,nowStatus);
                    break;
                default:
                    return new Response(HintMessageEunm.EXCEPTION_ERROR_DEFAULT);///操作失败
            }
            insActionLogService.InsOneActionLogService(actionNameStr,lastUpdateUid, ToolUtil.getClientIp(request),"管理员 {0} 在 {1} 进行了{2}操作，将id为{3}的数据从状态{4}修改为了{5}，修改结果为：{6}",actionAccount, ToolUtil.getStringTimeByDateTime(lastUpdateTime),actionNameStr,id,nowStatus,upStatus,saveRes);
            if(saveRes == 1){
                //操作成功
                return new Response(HintMessageEunm.EXCEPTION_SUCCESS_DEFAULT);
            }
            ///操作失败
            return new Response(HintMessageEunm.EXCEPTION_ERROR_DEFAULT);
        }catch (Exception e){
            //LOG
            logger.error(actionNameStr+"："+e);
            //提示出现异常
            return new Response(HintMessageEunm.EXCEPTION_ACTION_ABNORMAL_ERROR);
        }

    }




}
